package com.sms.controller;

import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import com.sms.common.A;
import com.sms.pojo.Products;
import com.sms.service.impl.ProductsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

//通过名称搜索商品
@WebServlet("/ProductsSearchByName")
public class ProductsSearchByNameController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String productName=req.getParameter("productName");
        ProductsServiceImpl service=new ProductsServiceImpl();
        List<Products> productList=service.listProductsByName(productName);
        service.getSqlSession().close();
        if (!productList.isEmpty()) {
            resp.getWriter().write(JSONUtil.toJsonStr(A.success("商品搜索成功", productList), JSONConfig.create().setDateFormat("yyyy-MM-dd")));
        } else {
            resp.getWriter().write(JSONUtil.toJsonStr(A.fail("商品搜索失败")));
        }
    }
}
